<?php
namespace app\know\validate;

use app\know\model\TeachingMaterialContentModel;
use app\know\validate\KnowBaseValidate;

class TeachingMaterialContentValidate extends KnowBaseValidate
{
    protected $rule = [
        'material_uuid' => 'require|length:32|checkKey',
        'chapter_uuid' => 'require|length:32',
        'content' => 'require|min:1',
        'id' => 'require|gt:0',
    ];

    protected $message = [
        'material_uuid.require' => '资料/教材为必填项',
        'material_uuid.length' => '资料/教材长度不合法',
        'chapter_uuid.require' => '章节为必填项',
        'chapter_uuid.length' => '章节长度不合法',
        'content.require' => '内容为必填项',
        'id.require' => 'id不能为空',
    ];

    protected $scene = [
        'add'  => ['material_uuid', 'chapter_uuid'],
        'edit' => ['id'],
        'delete' => ['id'],
    ];

    // 自定义验证规则
    // 同一类型下key不重复，value不重复
    protected function checkKey($value, $rule, $data)
    {
        $model = new TeachingMaterialContentModel();
        $wh = ['material_uuid' => $data['material_uuid'], 'chapter_uuid' => $data['chapter_uuid']];
        $item  = $model->where($wh)->find();

        if (empty($item)) 
		{
            return true;
        } 
		else
		{
            return '同一教材/资料，同一章节的内容已经存在';
        }
    }
	
}